package com.ilvyou.api.service;

import com.ilvyou.api.bean.result.BillDetail;
import com.ilvyou.api.bean.result.BillMonth;
import com.ilvyou.core.base.BaseService;
import com.ilvyou.core.dao.Criteria;
import com.ilvyou.core.dao.Dao;
import com.ilvyou.data.lhf.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by GuanYuCai on 2016/9/21 0021.
 */
@Service
public class BillService extends BaseService {

    @Resource(name = "lhfDao")
    private Dao lhfDao;

    @Override
    protected Dao getDao() {
        return lhfDao;
    }

    public String getTradeName(String type, long logid) throws Exception{
        String tradeName = "";
        if("Drawal".equals(type)){ //提现
            AlipayWithdrawEntity withdraw = lhfDao.get(AlipayWithdrawEntity.class, new Criteria("reflogid=?", logid));
            String bankCard = withdraw == null ? "" : withdraw.getBanknum();
            tradeName = bankCard.length() > 0 ? "尾号" + bankCard.substring(bankCard.length() - 4, bankCard.length()) : "";
        }else if("Recharge".equals(type)){ //充值
            AlipayRechargeEntity recharge = lhfDao.get(AlipayRechargeEntity.class, new Criteria("reflogid=?", logid));
            tradeName = recharge == null ? "" : recharge.getBankname();
        }else if(("Transfer").equals(type)){
            AlipayTransferEntity t = lhfDao.get(AlipayTransferEntity.class, new Criteria("outreflogid=? or inreflogid=?", logid, logid));
            if(t != null){
                tradeName = t.getInreflogid() == logid ? t.getFromcompanyshortname() : t.getTocompanyshortname();
            }
        }else if("Receive".equals(type)){ //收款
            AlipayReceiveEntity receive = lhfDao.get(AlipayReceiveEntity.class, new Criteria("reflogid=?", logid));
            if(receive != null){
                ScrAlipayEntity alipay = lhfDao.getByPK(ScrAlipayEntity.class, receive.getFactid());
                tradeName = alipay == null ? "" : alipay.getBppcompanyname();
            }
        }else if(("Pay").equals(type)){ //付款
            AlipayPaymentEntity payment = lhfDao.get(AlipayPaymentEntity.class, new Criteria("reflogid=?", logid));
            if(payment != null){
                ScrAlipayEntity alipay = lhfDao.getByPK(ScrAlipayEntity.class, payment.getTactid());
                tradeName = alipay == null ? "" : alipay.getBppcompanyname();
            }
        }
        return tradeName;
    }

    public BillDetail getBillDetail(String type, long logid, long actId) throws Exception{
        BillDetail billDetail = new BillDetail();

        if(("Drawal").equals(type)){ //提现
            AlipayWithdrawEntity withdraw = lhfDao.get(AlipayWithdrawEntity.class, new Criteria("reflogid=?", logid));
            if(withdraw != null){
                billDetail.setBankname(withdraw.getBankname());
                billDetail.setBankcard(withdraw.getBanknum());
                billDetail.setTradelogid(withdraw.getReflogid());
                billDetail.setAmount(withdraw.getAmount());
                billDetail.setStatus(withdraw.getTradestatus() != null ? withdraw.getTradestatus() : 1);
                billDetail.setTradedate(withdraw.getCreatedon());
                billDetail.setSubmittime(withdraw.getCreatedon());
                billDetail.setParsedate(withdraw.getParsedate());
                if(withdraw.getParsedate() != null && withdraw.getParsedate().after(withdraw.getLastedon())){
                    billDetail.setSuccesstime(withdraw.getParsedate());
                }else{
                    billDetail.setSuccesstime(withdraw.getLastedon());
                }
            }
        }else if(("Transfer").equals(type)){ //转账
            AlipayTransferEntity t = lhfDao.get(AlipayTransferEntity.class, new Criteria("outreflogid=? or inreflogid=?", logid, logid));
            if(t != null){
                billDetail.setTcompanyname(t.getInreflogid() == logid ? t.getFromcompanyshortname() : t.getTocompanyshortname());
                billDetail.setTradedate(t.getPaydate());
                billDetail.setTradelogid(logid);
                billDetail.setAmount(t.getPayamount());
                billDetail.setStatus(t.getTradestatus());
            }
        }else if(("Recharge").equals(type)){ //充值
            AlipayRechargeEntity recharge = lhfDao.get(AlipayRechargeEntity.class, new Criteria("reflogid=?", logid));
            if(recharge != null){
                billDetail.setBankname(recharge.getBankname());
                billDetail.setBankcard("");
                billDetail.setTradelogid(recharge.getReflogid());
                billDetail.setAmount(recharge.getAmount());
                billDetail.setStatus(recharge.getTradestatus());
                billDetail.setTradedate(recharge.getRechargedate());
            }
        }else if(("Receive".equals(type))){ //收款
            AlipayReceiveEntity receive = lhfDao.get(AlipayReceiveEntity.class, new Criteria("reflogid=?", logid));
            if(receive != null){
                billDetail.setOrderid(receive.getOrdernum());
                billDetail.setTradelogid(receive.getReflogid());
                billDetail.setTradedate(receive.getDrawaldate());
                billDetail.setAmount(receive.getDrawalamount());
                billDetail.setStatus(receive.getTradestatus());
                billDetail.setTcompanyname(receive.getOrderdigest());
            }

        }else if(("Pay").equals(type)){ //付款
            AlipayPaymentEntity payment = lhfDao.get(AlipayPaymentEntity.class, new Criteria("reflogid=?", logid));
            if(payment != null){
                billDetail.setOrderid(payment.getOrdernum());
                billDetail.setTradelogid(payment.getReflogid());
                billDetail.setTradedate(payment.getPaydate());
                billDetail.setAmount(payment.getPayamount());
                billDetail.setStatus(payment.getTradestatus());
                billDetail.setTcompanyname(payment.getOrderdigest());
            }
        }else{ //其他情况
            AlipayLogEntity log = getByPK(AlipayLogEntity.class, logid);
            ScrAlipayEntity alipay = getByPK(ScrAlipayEntity.class, actId);
            if(log != null){
                billDetail.setTcompanyname(alipay.getBppcompanyname());
                billDetail.setTradelogid(log.getLogid());
                billDetail.setAmount(log.getTradeamount());
                billDetail.setTradedate(log.getCreatedon());
                billDetail.setRemark(log.getTradedigest());
                billDetail.setStatus(Short.parseShort(log.getTradestatus()));
                billDetail.setOrderid(log.getOrdernum());
            }
        }
        return billDetail;
    }

    public BillMonth getMonthBill(long actId, int iMonth) throws Exception{

        //收款总额
        double receive = getAmount(actId, iMonth, "Receive", "");
        //充值总额
        double recharge = getAmount(actId, iMonth, "Recharge", "");
        //转入总额
        double transferIn = getAmount(actId, iMonth, "Transfer", "In");
        //付款总额
        double payment = getAmount(actId, iMonth, "Pay", "");
        //转出总额
        double transferOut = getAmount(actId, iMonth, "Transfer", "Out");
        //提现总额
        double withdraw = getAmount(actId, iMonth, "Drawal", "Out");

        BillMonth result = new BillMonth();
        result.setActid(actId);
        result.setPeriodmonth(iMonth);
        result.setReceive(receive);
        result.setRecharge(recharge);
        result.setTransferin(transferIn);
        result.setPayment(payment);
        result.setTransferout(transferOut);
        result.setWithdraw(withdraw);
        result.setTotalin(receive + recharge + transferIn);
        result.setTotalout(payment + transferOut + withdraw);
        return result;
    }

    private double getAmount(long actId, int iMonth, String type, String flow) throws Exception{
        Criteria criteria = new Criteria("actid=? and periodmonth=?", actId, iMonth)
                .add("tradestatus='2'");
        criteria.add("tradetype=?", type);
        criteria.add("tradeflow=?", flow);
        Map<String, Object> map = lhfDao.getMap(AlipayLogEntity.class, "coalesce(sum(tradeamount),0) as sum", criteria);
        return (Double) map.get("sum");
    }
}
